<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 分光器线
 *
 * @icon fa fa-circle-o
 */
class Luyougfen extends Backend
{

    /**
     * Luyougfen模型对象
     * @var \app\admin\model\Luyougfen
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Luyougfen;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $empList = (new \app\admin\model\Luyouemp)->field(['id', 'name'])->whereRaw("FIND_IN_SET('fgq', flag)")->select();
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $last = date('t');
            $list = $this->model
                ->where("")
                ->where('day <= '.$last)
                ->order('id','asc')
                ->paginate($limit);
            $input = input();
            if(isset($input['filter']) && !empty(json_decode($input['filter'],true))){
                $filter = json_decode($input['filter'],true);
                if(!empty($filter['day'])) {
                    list($s, $e) = explode(' - ', $filter['day']);
                    $daylist = getDatesBetween($s, $e);
                    //$list = $re;
                    $list = [];
                    foreach ($daylist as $k => $item) {
                        $list[$k]['day'] = $item;
                    }
                }
            }else{
                $new_list = [];
                foreach($list as $k1=>$v1){
                    $day = date('Y-m-').$v1->day;
                    $d = date('Y-m-d',strtotime($day));
                    $new_list[$k1]['day'] = $d;
                }
                $list = $new_list;
            }
            $qi_yi_arr = [];
            $qi_zhong_arr = [];
            $qi_zhuo_arr = [];
            $qi_wu_arr = [];
            $qi_yang_arr = [];
            $qiye_arr = [];

            $tp_yi_arr = [];
            $tp_zhong_arr = [];
            $tp_zhuo_arr = [];
            $tp_wu_arr = [];
            $tp_yang_arr = [];
            $tp_link_arr = [];

            $yi_yi_arr = [];
            $yi_zhong_arr = [];
            $yi_zhuo_arr = [];
            $yi_wu_arr = [];
            $yi_yang_arr = [];
            $yidong_arr = [];

            $total = ['day' => '总数: '];
            $typeMap = ['fen' => '14', 'tiaoxian' => '15', 'pixian' => '16'];
            foreach ($list as &$row) {
                $d = $row['day'];
                foreach ($typeMap as $type => $typeVal) {
                    $sum = 0;
                    foreach ($empList as $emp) {
                        $val = $this->cacl($d, $emp['name'], $typeVal);
                        $key = 'emp_' . $type . '_' . $emp['id'];
                        $row[$key] = $val;
                        $sum += $val;
                        if (!isset($total[$key])) {
                            $total[$key] = 0;
                        }
                        $total[$key] += $val;
                    }
                    $row[$type] = $sum;
                }
//                $qi_yi = $this->cacl($d,'易剑伟','14');
//                $row['qi_yi']=$qi_yi;
//                $qi_zhong = $this->cacl($d,'钟明锋','14');
//                $row['qi_zhong']=$qi_zhong;
//                $qi_zhuo = $this->cacl($d,'卓大旺','14');
//                $row['qi_zhuo']=$qi_zhuo;
//                $qi_wu = $this->cacl($d,'伍海鹏','14');
//                $row['qi_wu']=$qi_wu;
//                $qi_yang = $this->cacl($d,'杨宇航','14');
//                $row['qi_yang']=$qi_yang;
//                $qiye = $qi_yi+$qi_zhong+$qi_zhuo+$qi_wu+$qi_yang;
//                $row['fen'] = $qiye;
//
//                $tp_yi = $this->cacl($d,'易剑伟','15');
//                $row['tp_yi']=$tp_yi;
//                $tp_zhong = $this->cacl($d,'钟明锋','15');
//                $row['tp_zhong']=$tp_zhong;
//                $tp_zhuo = $this->cacl($d,'卓大旺','15');
//                $row['tp_zhuo']=$tp_zhuo;
//                $tp_wu = $this->cacl($d,'伍海鹏','15');
//                $row['tp_wu']=$tp_wu;
//                $tp_yang = $this->cacl($d,'杨宇航','15');
//                $row['tp_yang']=$tp_yang;
//                $tp_link = $tp_yi+$tp_zhong+$tp_zhuo+$tp_wu+$tp_yang;
//                $row['tiaoxian'] = $tp_link;
//
//                $yi_yi = $this->cacl($d,'易剑伟','16');
//                $row['yi_yi']=$yi_yi;
//                $yi_zhong = $this->cacl($d,'钟明锋','16');
//                $row['yi_zhong']=$yi_zhong;
//                $yi_zhuo = $this->cacl($d,'卓大旺','16');
//                $row['yi_zhuo']=$yi_zhuo;
//                $yi_wu = $this->cacl($d,'伍海鹏','16');
//                $row['yi_wu']=$yi_wu;
//                $yi_yang = $this->cacl($d,'杨宇航','16');
//                $row['yi_yang']=$yi_yang;
//                $yidong =  $yi_yi+$yi_zhong+$yi_zhuo+$yi_wu+$yi_yang;
//                $row['pixian'] = $yidong;
//
//                $qi_yi_arr[] = $qi_yi;
//                $qi_zhong_arr[] = $qi_zhong;
//                $qi_zhuo_arr[] = $qi_zhuo;
//                $qi_wu_arr[] = $qi_wu;
//                $qi_yang_arr[] = $qi_yang;
//                $qiye_arr[] = $qiye;
//
//                $tp_yi_arr[] = $tp_yi;
//                $tp_zhong_arr[] = $tp_zhong;
//                $tp_zhuo_arr[] = $tp_zhuo;
//                $tp_wu_arr[] = $tp_wu;
//                $tp_yang_arr[] = $tp_yang;
//                $tp_link_arr[] = $tp_link;
//
//                $yi_yi_arr[] = $yi_yi;
//                $yi_zhong_arr[] = $yi_zhong;
//                $yi_zhuo_arr[] = $yi_zhuo;
//                $yi_wu_arr[] = $yi_wu;
//                $yi_yang_arr[] = $yi_yang;
//                $yidong_arr[] = $yidong;
            }
            foreach ($typeMap as $type => $typeVal) {
                $total[$type] = array_sum(array_column($list, $type));
            }
            $list[] = $total;
//            if(!empty($list)){
//                $firstarr[]=[
//                    'day'          => '总数: ',
//                    'fen'          => float_two(array_sum($qiye_arr)),
//                    'tiaoxian'          => float_two(array_sum($tp_link_arr)),
//                    'pixian'          => float_two(array_sum($yidong_arr)),
//                    'qi_yi'          => float_two(array_sum($qi_yi_arr)),
//                    'qi_zhong'          => float_two(array_sum($qi_zhong_arr)),
//                    'qi_zhuo'          => float_two(array_sum($qi_zhuo_arr)),
//                    'qi_wu'          => float_two(array_sum($qi_wu_arr)),
//                    'qi_yang'          => float_two(array_sum($qi_yang_arr)),
//                    'tp_yi'          => float_two(array_sum($tp_yi_arr)),
//                    'tp_zhong'          => float_two(array_sum($tp_zhong_arr)),
//                    'tp_zhuo'          => float_two(array_sum($tp_zhuo_arr)),
//                    'tp_wu'          => float_two(array_sum($tp_wu_arr)),
//                    'tp_yang'          => float_two(array_sum($tp_yang_arr)),
//                    'yi_yi'          => float_two(array_sum($yi_yi_arr)),
//                    'yi_zhong'          => float_two(array_sum($yi_zhong_arr)),
//                    'yi_zhuo'          => float_two(array_sum($yi_zhuo_arr)),
//                    'yi_wu'          => float_two(array_sum($yi_wu_arr)),
//                    'yi_yang'          => float_two(array_sum($yi_yang_arr)),
//                ];
//                $list = array_merge($list, $firstarr);
//            }
            $result = array("total" => count($list), "rows" => $list);

            return json($result);
        }
        $this->view->assign('emp', json_encode($empList));
        return $this->view->fetch();
    }

    protected function cacl($d,$user,$luyou_id,$new_hw=0)
    {
        if($new_hw){
            $ronghe = Db::table('fa_luyoukc')
                ->where('opt_date',$d)
                ->where('user',$user)
                ->where('luyou_id',$luyou_id)
                ->where('new_hw',$new_hw)
                //->whereNull('deletetime')
                ->sum('num');
        }else{
            $ronghe = Db::table('fa_luyoukc')
                ->where('user',$user)
                ->where('luyou_id',$luyou_id)
                ->where('opt_date',$d)
                //->whereNull('deletetime')
                ->sum('num');
        }

        return $ronghe==0?'0':float_two($ronghe);
    }



}
